﻿namespace SERFLITEMAX20.WinUI.Controls
{
    using SERFLITEMAX20;
    using SERFLITEMAX20.DataObjects;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class MyGameObjectList : UserControl
    {
        private Button btnFind;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ColumnHeader columnHeader3;
        private ColumnHeader columnHeader4;
        private ColumnHeader columnHeader5;
        private ColumnHeader columnHeader6;
        private IContainer components = null;
        private ListView lstView;
        private GameObjects mGameObjects = null;
        private ListViewSortManager mListViewSortManager = null;
        private ContextMenuStrip mnuMain;
        private ToolStripMenuItem mnuMainAssignToGroup;
        private ToolStripMenuItem mnuMainClearFiltered;
        private ToolStripMenuItem mnuMainClearSpecial;
        private ToolStripMenuItem mnuMainSetFiltered;
        private ToolStripMenuItem mnuMainSetSpecial;
        private EventHandler onSubItemMenueClick;
        private Panel panel1;
        private ToolStripMenuItem setSubTypeToolStripMenuItem;
        private TextBox txtFindText;

        public MyGameObjectList()
        {
            this.InitializeComponent();
            this.onSubItemMenueClick = (EventHandler) Delegate.Combine(this.onSubItemMenueClick, new EventHandler(this.SubItemMenueClick));
        }

        private void AddItem(GameObject pGameObject)
        {
            if (pGameObject.name.Length > 0)
            {
                ListViewItem item = this.lstView.Items.Add(pGameObject.entry.ToString());
                item.Tag = pGameObject;
                item.SubItems.Add(pGameObject.name);
                item.SubItems.Add(((GameobjectTypes) pGameObject.typeid).ToString());
                item.SubItems.Add(((enNodeType) pGameObject.subtype).ToString());
                item.SubItems.Add(pGameObject.filtered.ToString());
                item.SubItems.Add(pGameObject.special.ToString());
                if (pGameObject.filtered)
                {
                    item.ForeColor = Color.Red;
                }
                else if (pGameObject.special)
                {
                    item.ForeColor = Color.Green;
                }
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (this.txtFindText.Text.Length > 0)
            {
                this.lstView.ListViewItemSorter = null;
                this.InitListViewSorter();
                this.lstView.Items.Clear();
                if (this.mGameObjects != null)
                {
                    foreach (GameObject obj2 in this.mGameObjects.GameObjectItemsArray)
                    {
                        if (obj2.name.IndexOf(this.txtFindText.Text, StringComparison.CurrentCultureIgnoreCase) != -1)
                        {
                            this.AddItem(obj2);
                        }
                    }
                    this.lstView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[3].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[4].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[5].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                }
            }
            else
            {
                this.lstView.ListViewItemSorter = null;
                this.InitListViewSorter();
                this.lstView.Items.Clear();
                this.Initialize(this.mGameObjects);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public void Initialize(GameObjects pGameObjects)
        {
            this.lstView.ListViewItemSorter = null;
            this.mListViewSortManager = null;
            this.lstView.Items.Clear();
            this.mGameObjects = pGameObjects;
            foreach (GameObject obj2 in this.mGameObjects.GameObjectItemsArray)
            {
                this.AddItem(obj2);
            }
            this.lstView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            this.lstView.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            this.lstView.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            this.lstView.Columns[3].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            this.lstView.Columns[4].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            this.lstView.Columns[5].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            this.InitListViewSorter();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.mnuMainClearFiltered = new System.Windows.Forms.ToolStripMenuItem();
            this.lstView = new System.Windows.Forms.ListView();
            this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
            this.mnuMain = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.mnuMainSetFiltered = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuMainSetSpecial = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuMainClearSpecial = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuMainAssignToGroup = new System.Windows.Forms.ToolStripMenuItem();
            this.setSubTypeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.btnFind = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.txtFindText = new System.Windows.Forms.TextBox();
            this.mnuMain.SuspendLayout();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // mnuMainClearFiltered
            // 
            this.mnuMainClearFiltered.Name = "mnuMainClearFiltered";
            this.mnuMainClearFiltered.Size = new System.Drawing.Size(163, 22);
            this.mnuMainClearFiltered.Text = "Clear Filtered";
            // 
            // lstView
            // 
            this.lstView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader4,
            this.columnHeader1,
            this.columnHeader2,
            this.columnHeader3,
            this.columnHeader5,
            this.columnHeader6});
            this.lstView.ContextMenuStrip = this.mnuMain;
            this.lstView.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lstView.FullRowSelect = true;
            this.lstView.HideSelection = false;
            this.lstView.Location = new System.Drawing.Point(0, 34);
            this.lstView.Name = "lstView";
            this.lstView.Size = new System.Drawing.Size(585, 211);
            this.lstView.TabIndex = 4;
            this.lstView.UseCompatibleStateImageBehavior = false;
            this.lstView.View = System.Windows.Forms.View.Details;
            // 
            // columnHeader4
            // 
            this.columnHeader4.Text = "Entry";
            this.columnHeader4.Width = 128;
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Name";
            this.columnHeader1.Width = 98;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "TypeID";
            this.columnHeader2.Width = 98;
            // 
            // columnHeader3
            // 
            this.columnHeader3.Text = "SubType";
            this.columnHeader3.Width = 107;
            // 
            // columnHeader5
            // 
            this.columnHeader5.Text = "filtered";
            // 
            // columnHeader6
            // 
            this.columnHeader6.Text = "special";
            // 
            // mnuMain
            // 
            this.mnuMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mnuMainSetFiltered,
            this.mnuMainSetSpecial,
            this.mnuMainClearFiltered,
            this.mnuMainClearSpecial,
            this.mnuMainAssignToGroup,
            this.setSubTypeToolStripMenuItem});
            this.mnuMain.Name = "contextMenuStrip1";
            this.mnuMain.Size = new System.Drawing.Size(164, 136);
            // 
            // mnuMainSetFiltered
            // 
            this.mnuMainSetFiltered.Name = "mnuMainSetFiltered";
            this.mnuMainSetFiltered.Size = new System.Drawing.Size(163, 22);
            this.mnuMainSetFiltered.Text = "Set Filtered";
            // 
            // mnuMainSetSpecial
            // 
            this.mnuMainSetSpecial.Name = "mnuMainSetSpecial";
            this.mnuMainSetSpecial.Size = new System.Drawing.Size(163, 22);
            this.mnuMainSetSpecial.Text = "Set Special";
            // 
            // mnuMainClearSpecial
            // 
            this.mnuMainClearSpecial.Name = "mnuMainClearSpecial";
            this.mnuMainClearSpecial.Size = new System.Drawing.Size(163, 22);
            this.mnuMainClearSpecial.Text = "Clear Special";
            // 
            // mnuMainAssignToGroup
            // 
            this.mnuMainAssignToGroup.Name = "mnuMainAssignToGroup";
            this.mnuMainAssignToGroup.Size = new System.Drawing.Size(163, 22);
            this.mnuMainAssignToGroup.Text = "Assign To Group";
            // 
            // setSubTypeToolStripMenuItem
            // 
            this.setSubTypeToolStripMenuItem.Name = "setSubTypeToolStripMenuItem";
            this.setSubTypeToolStripMenuItem.Size = new System.Drawing.Size(163, 22);
            this.setSubTypeToolStripMenuItem.Text = "Set Sub Type";
            this.setSubTypeToolStripMenuItem.DropDownOpening += new System.EventHandler(this.setSubTypeToolStripMenuItem_DropDownOpening);
            // 
            // btnFind
            // 
            this.btnFind.Location = new System.Drawing.Point(237, 5);
            this.btnFind.Name = "btnFind";
            this.btnFind.Size = new System.Drawing.Size(117, 24);
            this.btnFind.TabIndex = 1;
            this.btnFind.Text = "Find";
            this.btnFind.UseVisualStyleBackColor = true;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btnFind);
            this.panel1.Controls.Add(this.txtFindText);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(585, 34);
            this.panel1.TabIndex = 5;
            // 
            // txtFindText
            // 
            this.txtFindText.Location = new System.Drawing.Point(61, 8);
            this.txtFindText.Name = "txtFindText";
            this.txtFindText.Size = new System.Drawing.Size(154, 20);
            this.txtFindText.TabIndex = 0;
            // 
            // MyGameObjectList
            // 
            this.Controls.Add(this.lstView);
            this.Controls.Add(this.panel1);
            this.Name = "MyGameObjectList";
            this.Size = new System.Drawing.Size(585, 245);
            this.mnuMain.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        private void InitListViewSorter()
        {
            this.mListViewSortManager = new ListViewSortManager(this.lstView, new System.Type[] { typeof(ListViewInt32Sort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort) });
        }

        private void mnuMainAssignToGroup_Click(object sender, EventArgs e)
        {
        }

        private void mnuMainClearFiltered_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lstView.SelectedItems)
            {
                GameObject tag = (GameObject) item.Tag;
                item.SubItems[4].Text = false.ToString();
                tag.filtered = false;
                if (tag.special)
                {
                    item.ForeColor = Color.Green;
                }
                else
                {
                    item.ForeColor = SystemColors.WindowText;
                }
            }
        }

        private void mnuMainClearSpecial_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lstView.SelectedItems)
            {
                GameObject tag = (GameObject) item.Tag;
                item.SubItems[5].Text = false.ToString();
                tag.special = false;
                if (!tag.filtered)
                {
                    item.ForeColor = SystemColors.WindowText;
                }
            }
        }

        private void mnuMainSetFiltered_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lstView.SelectedItems)
            {
                GameObject tag = (GameObject) item.Tag;
                item.SubItems[4].Text = true.ToString();
                tag.filtered = true;
                item.ForeColor = Color.Red;
            }
        }

        private void mnuMainSetSpecial_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lstView.SelectedItems)
            {
                GameObject tag = (GameObject) item.Tag;
                bool flag = true;
                item.SubItems[4].Text = flag.ToString();
                item.SubItems[5].Text = true.ToString();
                tag.special = true;
                if (item.ForeColor != Color.Red)
                {
                    item.ForeColor = Color.Green;
                }
            }
        }

        private void setSubTypeToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            if (this.setSubTypeToolStripMenuItem.DropDownItems.Count == 0)
            {
                this.setSubTypeToolStripMenuItem.DropDownItems.Add("None", null, this.onSubItemMenueClick).Tag = -1;
                this.setSubTypeToolStripMenuItem.DropDownItems.Add("Unknown", null, this.onSubItemMenueClick).Tag = 0;
                this.setSubTypeToolStripMenuItem.DropDownItems.Add("Mine", null, this.onSubItemMenueClick).Tag = 1;
                this.setSubTypeToolStripMenuItem.DropDownItems.Add("Herb", null, this.onSubItemMenueClick).Tag = 2;
                this.setSubTypeToolStripMenuItem.DropDownItems.Add("Chest", null, this.onSubItemMenueClick).Tag = 3;
                this.setSubTypeToolStripMenuItem.DropDownItems.Add("Fish", null, this.onSubItemMenueClick).Tag = 4;
                this.setSubTypeToolStripMenuItem.DropDownItems.Add("Quest", null, this.onSubItemMenueClick).Tag = 5;
            }
        }

        private void SubItemMenueClick(object sender, EventArgs e)
        {
            int tag = (int) ((ToolStripItem) sender).Tag;
            foreach (ListViewItem item in this.lstView.SelectedItems)
            {
                GameObject obj2 = (GameObject) item.Tag;
                obj2.subtype = tag;
                item.SubItems[3].Text = ((enNodeType) tag).ToString();
            }
        }
    }
}

